<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;


class goods extends BaseController
{

    //商品列表页
    public function good_list()
    {
        $tname=Request::get('tname');
        $cid=Request::get('cid','','trim,htmlspecialchars,strip_tags');
        $keywords=Request::get('keywords','','trim,htmlspecialchars,strip_tags');
        $if_show=Request::get('if_show','','trim,htmlspecialchars,strip_tags');
        $recommended=Request::get('recommended/a',[],'trim,htmlspecialchars,strip_tags');
        if($tname=='上架时间'){
            $tname='上架升序';
            $order="add_time asc";
        }elseif($tname=='上架升序'){
            $tname='上架降序';
            $order="add_time desc";
        }else{
            $tname='上架时间';
            $order="goods_id";
        }
        $db=Db::table('ecm_goods')->field(true);
        if(''!==$keywords){
            $db->where('goods_name','like','%'.$keywords.'%');
        }
        if(''!=$if_show){
            $db->where('if_show',$if_show);
        }
        if(''!=$cid){
            $db->where('brand',$cid);
        }
        if(count($recommended)==1 and count($recommended)!=0){
            $db->where('recommended',$recommended[0]);
        }
        $rt=$db->order($order)
            ->select();

        $brand=Db::table('ecm_brand')->field('tag')->select()->toArray();
        $qwe=array();
        foreach ($brand  as  $v ){
            if(!in_array($v['tag'],$qwe)){
                array_push($qwe,$v['tag']);
            }
        }

        $asd=[];
        $brand=Db::table('ecm_brand')->field('brand_id,brand_name,tag')->select()->toArray();
        foreach ($qwe as $k=>$v){
            foreach ($brand as $i){
                if($i['tag']==$v){
                    $asd[$k]['class']=$v;
                    $asd[$k]['brand'][]=$i;

                }
            }

        }

        View::assign('asd',$asd);
        View::assign('cid',$cid);
        View::assign('keywords',$keywords);
        View::assign('if_show',$if_show);
        View::assign('recommended',$recommended);
        View::assign('rt',$rt);
        View::assign('timer',$tname);
        return View::fetch();
    }

    //商品添加页
    public function good_add()
    {
        return View::fetch();
    }


}